﻿#include <opencv2/opencv.hpp>
#include <iostream>
#include "putText.h"
using namespace std;
using namespace cv;

//掩膜操作对比度
int test002() {
	Mat src, dst ,dst2;
	src = imread("D:/图/TCP118v1_by_Tiziano_Consonni.jpg");
	if (!src.data) {
		printf("could not load image...\n");
		return -1;
	}
	namedWindow("input image", CV_WINDOW_AUTOSIZE);
	imshow("input image", src);

	
	int cols = (src.cols-1) * src.channels();
	int offsetx = src.channels();
	int rows = src.rows;

	dst = Mat::zeros(src.size(), src.type());
	dst2 = Mat::zeros(src.size(), src.type());
	//for (int row = 1; row < (rows - 1); row++) {
	//	const uchar* previous = src.ptr<uchar>(row - 1);
	//	const uchar* current = src.ptr<uchar>(row);
	//	const uchar* next = src.ptr<uchar>(row + 1);
	//	uchar* output = dst.ptr<uchar>(row);
	//	for (int col = offsetx; col < cols; col++) {
	//		output[col] = saturate_cast<uchar>(5 * current[col] - (current[col- offsetx] + current[col+ offsetx] + previous[col] + next[col]));
	//	}
	//}

	//for (int row = 1; row < (rows - 1); row++) {
	//	const uchar* previous = src.ptr<uchar>(row - 1);
	//	const uchar* current = src.ptr<uchar>(row);
	//	const uchar* next = src.ptr<uchar>(row + 1);
	//	uchar* output = dst2.ptr<uchar>(row);
	//	for (int col = offsetx; col < cols; col++) {
	//		output[col] = saturate_cast<uchar>(4.5 * current[col] - (current[col - offsetx] + current[col + offsetx] + previous[col] + next[col]));
	//	}
	//}

	
	double t = getTickCount();
	Mat kernel = (Mat_<char>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	Mat kernel2 = (Mat_<char>(3, 3) << 0, -1, 0, -1, 4.5, -1, 0, -1, 0);
	filter2D(src, dst, src.depth(), kernel);
	filter2D(src, dst2, src.depth(), kernel2);
	double timeconsume = (getTickCount() - t) / getTickFrequency();
	printf("tim consume %.2f\n", timeconsume);

	namedWindow("contrast image demo", CV_WINDOW_AUTOSIZE);
	imshow("contrast image demo", dst);

	namedWindow("contrast image demo2", CV_WINDOW_AUTOSIZE);

	imshow("contrast image demo2", dst2);
	waitKey(0);
}

void test001() {
	Mat src = imread("C:/Users/Lenovo/Desktop/图/640.webp.jpg");
	if (src.empty()) {
		cout << "没有找到文件" << endl;
	}
	namedWindow("test", CV_WINDOW_AUTOSIZE);
	imshow("test", src);
	waitKey(0);
}

//灰度
void testGray(){

	Mat src, imgGray;
	src = imread("D:/图/captcha.jpg");
	if (src.empty()) {
		cout << "没有找到文件" << endl;
	}

	namedWindow("src", CV_WINDOW_NORMAL);
	imshow("src", src);

	cvtColor(src, imgGray, CV_BGR2GRAY);
	namedWindow("camera", CV_WINDOW_NORMAL);
	imshow("camera", imgGray);
}

//通道数
void testChel() {
	Mat src;
	src = imread("D:/图/clock_by_Bernhard_Hanakam.jpg");
	if (src.empty()) {
		cout << "没有找到文件" << endl;
	}
	int i= src.channels();
	cout << i << endl;
}

void testMat() {
	Mat dsc = Mat::zeros(300,300, CV_8UC1);
	Mat mo = Mat::ones(200, 200, CV_32F);
	Mat me = Mat::eye(400, 400, CV_64F);
	imshow("camera", me);
}

void MyLines() {
	Mat src = imread("D:/图/clock_by_Bernhard_Hanakam.jpg");
	Mat dsc = Mat::zeros(300, 300, CV_8UC1);
	Point p1 = Point(20, 30);
	Point p2;
	p2.x = 400;
	p2.y = 400;
	Scalar color = Scalar(255,255,255);
	Scalar color2 = Scalar(0, 0, 0);
	dsc = color;
	line(dsc, p1, p2, color2, 1, LINE_AA);
	imshow("camera", dsc);
}

void testText() {
	Mat img(150, 600, CV_8UC3, Scalar(255, 255, 255));//初始化图像
	putTextZH(img, "力拔山兮气盖世", Point(30, 30), Scalar(0, 0, 0), 80, "微软雅黑");
	imwrite("1.png", img);
	imshow("", img);

}

//二值化
void testThreshold() {
	Mat src, result, imgGray;
	src = imread("D:/图/captcha.jpg");
	if (src.empty()) {
		cout << "没有找到文件" << endl;
	}

	cvtColor(src, imgGray, CV_BGR2GRAY);
	// 转为二值图  
	threshold(imgGray, result, 100, 255, CV_THRESH_BINARY);

	//adaptiveThreshold需先灰度
	adaptiveThreshold(imgGray, result,255, ADAPTIVE_THRESH_GAUSSIAN_C,THRESH_BINARY, 19, 1);
	//显示原图  
	namedWindow("Image");
	imshow("Image", src);
	// 显示二值图 
	namedWindow("二值化后的图像1");
	imshow("二值化后的图像1", result);
}

//去干扰
void test003() {
	Mat src, dst;
	src = imread("D:/图/captcha.jpg");
	if (!src.data) {
		printf("could not load image...\n");
	}

	char INPUT_WIN[] = "input image";
	char OUTPUT_WIN[] = "result image";
	namedWindow(INPUT_WIN, CV_WINDOW_AUTOSIZE);
	imshow(INPUT_WIN, src);

	Mat gray_src;
	cvtColor(src, gray_src, CV_BGR2GRAY);
	imshow("gray image", gray_src);

	Mat binImg;
	adaptiveThreshold(~gray_src, binImg, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, 15, -2);
	imshow("binary image", binImg);

	// 水平结构元素
	Mat hline = getStructuringElement(MORPH_RECT, Size(src.cols / 16, 1), Point(-1, -1));
	// 垂直结构元素
	Mat vline = getStructuringElement(MORPH_RECT, Size(1, src.rows / 16), Point(-1, -1));
	// 矩形结构
	Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3), Point(-1, -1));

	Mat temp;
	erode(binImg, temp, kernel);
	dilate(temp, dst, kernel);
	// morphologyEx(binImg, dst, CV_MOP_OPEN, vline);
	bitwise_not(dst, dst);
	//blur(dst, dst, Size(3, 3), Point(-1, -1));
	imshow("Final Result", dst);
}


int main()
{
	test003();
	waitKey(0);
	return 0;

}